package com.augurit.common.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.entity.AtHomepageConfig;
import com.augurit.common.system.convert.AtHomepageConfigConverter;
import com.augurit.common.system.web.org.form.AtHomepageConfigForm;

/**
 * 系统首页配置DAO类
 */
@Repository
public class AtHomepageConfigDao extends BaseDao<AtHomepageConfig, Long> {

	/**
	 * 获取启用的首页配置Form对象
	 * @return
	 */
	public List<AtHomepageConfigForm> getActiveHomepages(){
		String hql = "from AtHomepageConfig ps where ps.isActive='1' order by ps.homepageCode";
		List<AtHomepageConfig> list = this.find(hql);
		return AtHomepageConfigConverter.convertToFormList(list);
	}
	
	public AtHomepageConfigForm getForm(Long id){
		AtHomepageConfigForm form = null;
		
		if(id != null){
			AtHomepageConfig entity = this.get(id);
			if(entity != null)
				form = AtHomepageConfigConverter.convertToForm(entity);
		}

		return form;
	}
	
	/**
	 * 保存应用系统Form对象
	 * @param form 应用系统Form对象
	 */
	public void save(AtHomepageConfigForm form){
		
		if(form != null){
			AtHomepageConfig entity = null;
			
			//准备VO对象
			if(form != null && form.getHcId() != null && form.getHcId().intValue() > 0){
				entity = this.get(form.getHcId());
			}else{
				form.setHcId(null);
				entity = new AtHomepageConfig();
			}
			
			//属性值转换
			AtHomepageConfigConverter.convertToEntity(form, entity);
			
			//保存
			this.save(entity);
			
			//回填ID属性值
			form.setHcId(entity.getHcId());
		}
	}

}
